package com.kfm.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.Room;
import com.kfm.hotel.domain.RoomType;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.RoomService;
import com.kfm.hotel.service.RoomTypeService;
import com.kfm.hotel.service.SysDictService;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService service;
    @Autowired
    private RoomTypeService roomTypeService;
    @Autowired
    private SysDictService sysDictService;




    @GetMapping
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("room/index");
        return view;
    }
    /**
     * 分页
     */
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit,
                  Room empty){
        //后端实现分页
        PageInfo<Room> list = service.list(empty, page, limit);
        return R.ok(list);
    }

    /**
     * 添加
     * @return
     */
    @GetMapping("/add")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("room/add");
        //房间类型
        modelAndView.addObject("roomTypes",roomTypeService.getRoomTypes());
        //楼层:遍历sys_dict表中的类型为2的数据，即有楼层的数据
        modelAndView.addObject("floors",sysDictService.getByType(2));
        return modelAndView;
    }
   @PostMapping("/add")
    public R add(@RequestBody Room room) throws Exception{
       boolean save = service.save(room);
       if(save){
           return R.ok("添加成功");
       }else {
           return R.fail("添加失败");
       }

   }
    /**
     * 修改房间请求
     */
    @GetMapping("/edit{id}")
    public ModelAndView edit(@PathVariable("id") Integer id){
        ModelAndView modelAndView03 = new ModelAndView("room/edit");
        //在后端数据库中查询
        Room byId = service.getById(id);
        //查询到的信息放到视图中
        modelAndView03.addObject("room",byId);
        return modelAndView03;
    }
    @PostMapping("/edit")
    public R edit(@RequestBody Room room){
        boolean update01 = service.updateById(room);
        if(update01){
            return R.ok("修改成功");
        }else{
            return R.fail("修改失败");
        }
    }
    @GetMapping("/get/{id}")
    public R getById(@PathVariable Integer id){
        return R.ok(service.getByIdDeep(id));
    }
//
//    /**
//     * 批量删除房间请求
//     * @param ids
//     * @return
//     */
//    @DeleteMapping("/batchDelete")
//    public R batchDelete(@RequestBody List<Integer> ids){
//        List<Room> list = new ArrayList<>();
//        //遍历集合
//        for (Integer id:ids) {
//            //实例化对象
//            Room room01 = new Room();
//            //确认删除
//            room01.setIsDel(true);
//            room01.setId(id);
//            list.add(room01);
//        }
//        //更新id
//        boolean b = service.updateBatchById(list);
//        if(b){
//            return R.ok("删除成功！");
//        }else {
//            return R.fail("删除失败！");
//        }
//    }
    /**
     * 测试批量删除
     * 1.id中必须有值
     * 2.Sysuser中必须添加有参构造方法
     */
    @PostMapping("/test")
    public R test(@RequestBody List<Room> room){
        boolean b = service.updateBatchById(room);
        if(b){
            return R.ok("删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }


}
